import java.util.Queue;
import java.util.LinkedList;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-28
 * Time: 14:06
 */
class MyStack {
    private Queue<Integer> queue1;
    private Queue<Integer> queue2;

    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if(!queue1.isEmpty()){
            queue1.offer(x);
        }else if(!queue2.isEmpty()){
            queue2.offer(x);
        }else{
            queue1.offer(x);
        }
    }

    public int pop() {
        if(empty()){
            return -1;
        }
        if(!queue1.isEmpty()){
            int size = queue1.size();
            for(int i=0;i<size-1;i++){
                int value = queue1.poll();
                queue2.offer(value);
            }
            return queue1.poll();
        }
        if(!queue2.isEmpty()){
            int size = queue2.size();
            for(int i=0;i<size-1;i++){
                int value = queue2.poll();
                queue1.offer(value);
            }
            return queue2.poll();
        }
        return -1;
    }

    public int top() {
        if(empty()){
            return -1;
        }
        int value = -1;
        if(!queue1.isEmpty()){
            int size = queue1.size();
            for(int i=0;i<size;i++){
                value = queue1.poll();
                queue2.offer(value);
            }
            return value;
        }
        if(!queue2.isEmpty()){
            int size = queue2.size();
            for(int i=0;i<size;i++){
                value = queue2.poll();
                queue1.offer(value);
            }
            return value;
        }
        return -1;
    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}